package com.mhys.hms.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhys.hms.entity.FeedBackObject;
import com.mhys.hms.entity.Recruit;
import com.mhys.hms.service.user.RecrManagerService;

/**
 * 部门管理controller
 * 
 * @author
 *
 */
@Controller
public class RecruitManager {
	@Autowired
	private RecrManagerService recrManagerService;

	/**
	 * 模糊分页查询
	 * 
	 * @param pageSize
	 * @param pageNumber
	 * @param deptName
	 * @return
	 */
	@RequestMapping(value = "/queryRecrInfo", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public FeedBackObject queryRecrInfo(int pageSize, int pageNumber, String recruitJob, String education) {
		FeedBackObject fbo = new FeedBackObject();

		// 分页查询数据
		List<Recruit> list = recrManagerService.getRecrInfo(pageSize, pageNumber, recruitJob, education);
		// 封装数据
		Map<String, Object> map = new HashMap<String, Object>();
		// 返回总页数
		map.put("total", recrManagerService.getRecrTotalNum(recruitJob, education));
		// 返回总数据
		map.put("data", list);
		fbo.Obj = map;
		return fbo;
	}

	@RequestMapping(value = "/queryRecrDetail", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public FeedBackObject queryRecrDetail(int recrId) {
		FeedBackObject fbo = new FeedBackObject();
		List<Recruit> list = recrManagerService.getRecrById(recrId);
		fbo.Obj = list;
		return fbo;
	}

	@RequestMapping(value = "/delRecrInfo", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public FeedBackObject delRecrInfo(int recrId) {
		FeedBackObject fbo = new FeedBackObject();
		int row = recrManagerService.deleteRecrById(recrId);
		if (row > 0) {
			fbo.success = "OK";
		} else {
			fbo.msg = "删除失败";
		}
		return fbo;
	}

	@RequestMapping(value = "/addRecrInfo", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public FeedBackObject addRecrInfo(HttpServletRequest req) {
		FeedBackObject fbo = new FeedBackObject();
		// 获取参数
		Recruit recr = new Recruit();
		recr.setAge(Integer.parseInt(req.getParameter("age")));
		recr.setBirthday(initDate(req.getParameter("birthday")));
		recr.setEducation(req.getParameter("education"));
		recr.setExperience(req.getParameter("experience"));
		recr.setHomeAddr(req.getParameter("homeAddr"));
//		recr.setId(id);
		recr.setName(req.getParameter("recrName"));
		recr.setPhone(req.getParameter("phone"));
		recr.setProfessional(req.getParameter("professional"));
		recr.setRecruitJob(req.getParameter("recruitJob"));
		recr.setRegistTime(initDate(req.getParameter("registTime")));
		recr.setRemark(req.getParameter("remark"));
		recr.setSchool(req.getParameter("school"));
		recr.setSex(req.getParameter("sex"));

		int row = recrManagerService.addRecrInfo(recr);
		if (row > 0) {
			fbo.success = "OK";
		} else {
			fbo.msg = "添加失败";
		}
		return fbo;
	}

	// 日期格式化
	public Date initDate(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}

}
